package ah.filesplit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*
 * 文件分割 Buffer
 */
public class FileSplit2 implements ActionListener{
	private JFrame jf = new JFrame("File Split");
	private String file = null;
	JTextField jtf = new JTextField("", 28);//分割文件
	JTextField jtf1 = new JTextField("", 21); //分割大小
	JTextField jtf2 = new JTextField("", 22); //合并文件文本框
	JTextField fileSize = new JTextField("", 27);
	String str = null;
	
	public FileSplit2()  {
		JFrame jf = new JFrame("File Split2");
		JPanel jp = new JPanel();
		JButton jb1 = new JButton("浏览");
		JButton jb2 = new JButton("分割");
		JButton jb4 = new JButton("添加");
		JButton jb3 = new JButton("合并");
		jp.add(new JLabel("        选择要分割的文件:        "));
		jp.add(jtf);
		jp.add(jb1);
		jp.add(new JLabel("文件大小(K):"));
		fileSize.setEditable(false);
		jp.add(fileSize);
		jp.add(new JLabel("分割大小(K):"));
		jp.add(jtf1);
		jp.add(jb2);
		jp.add(new JLabel("合并文件:"));
		jp.add(jtf2);
		jp.add(jb4);
		jp.add(jb3);
		jf.add(jp);
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		jb4.addActionListener(this);
		jf.setSize(400, 220);
		jf.setLocation(300, 200);
		jf.setResizable(false);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new FileSplit2();
	}

	public void actionPerformed(ActionEvent e) {
		String comm = e.getActionCommand();
		if("浏览".equals(comm)) {
			JFileChooser jfc = new JFileChooser();
			jfc.showOpenDialog(jf);
			jfc.setVisible(true);
			
			file = jfc.getSelectedFile().getAbsolutePath();
			fileSize.setText("" + (new File(file).length()+1023)/1024);
			jtf.setText(file);
		}
		else if("分割".equals(comm)) {
			long fileSize = Long.parseLong(this.fileSize.getText());
			long splitSize = Long.parseLong(jtf1.getText())*1024;
			file = jtf.getText();
			try {
				long startTime = System.currentTimeMillis();
				FileInputStream fis = new FileInputStream(file);
				FileOutputStream fos = null;
				FileChannel fci = fis.getChannel();
				ByteBuffer bb = ByteBuffer.allocate(4086);
				long len = 0;
				int i = 0;
				long j = 0;
				//byte[] b = new byte[1024];
				while(fis.available()>0) {
					fos = new FileOutputStream(file + ".part" + i);
					FileChannel fco = fos.getChannel();
					j = 0;
					while(j < splitSize) {
						bb.clear();
						len = fci.read(bb);
						if(len < 1) {
							break;
						}
						bb.flip();
						fco.write(bb);
						j += len;
					}
					i++;
					fos.close();
					fco.close();
				}
				fis.close();
				fci.close();
				System.out.println("split over " + (System.currentTimeMillis() - startTime)/1000);
			} catch (Exception e1) {
				e1.printStackTrace();
			}	
		}
		else if("添加".equals(comm)) {
			JFileChooser jfc = new JFileChooser();
			jfc.setMultiSelectionEnabled(true);  //放在open前面
			jfc.showOpenDialog(jf);		
			jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			jfc.setVisible(true);
			File[] files = jfc.getSelectedFiles();
			int a = files[0].getName().indexOf(".part");
			jtf2.setText(files[0].getName().substring(0, a));
			str = files[0].getParent()+ File.separator + jtf2.getText();
		}
		else if("合并".equals(comm)) {
			int i = 0;
			long len = 0;
			try {
				FileOutputStream fos = new FileOutputStream(str);
				FileChannel fc1 = fos.getChannel();
				while(new File(str+".part"+i).isFile()){
					FileInputStream fis = new FileInputStream(str+".part" +i);
					FileChannel fc2 = fis.getChannel();
					ByteBuffer bb = ByteBuffer.allocate(40860);
					//MappedByteBuffer bb = fc2.map(MapMode.READ_WRITE, 0, 1024);
					//byte[] b = new byte[1024];
					while(true){
						bb.clear();
						len = fc2.read(bb);
						if(len==-1) {
							break;
						}
						bb.flip();
						fc1.write(bb);
					}	
					fis.close();
					i++;
				}
				fos.close();
			
			} catch (Exception e1) {
				e1.printStackTrace();
			}		
		}
	}

}
